package com.demo2do.boyle.backend.service.report.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.report.ReportAduserService;
import com.demo2do.boyle.backend.service.report.impl.utils.DateUtils;
import com.demo2do.boyle.common.entity.aduser.Aduser;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.core.persistence.GenericJdbcSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author Jason.Y
 */
@Service("reportAduserService")
public class ReportAduserServiceImpl extends GenericServiceImpl<T> implements ReportAduserService {
	
	@Autowired 
	private GenericJdbcSupport genericJdbcSupport;
	
	/*
     * (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.AdvertisementService#count()
     */
	public int count() {
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Advertisement");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.AdvertisementService#count(com.demo2do.boyle.common.entity.advertisement.Status)
	 */
	public int count(String period) {
		Map<String, Object> parameters = new HashMap<String, Object>(1);
		parameters.put("period", period);
		
		StringBuffer sentence = new StringBuffer();
		sentence.append(" SELECT COUNT(1) FROM aduser ");
		sentence.append(" WHERE aduser.status = 1 ");

		DateUtils dateUtils = new DateUtils();
		
		if (StringUtils.isEmpty(period) || "MONTH".equals(period)) {
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentMonthStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentMonthEndTime() +"' ");
			
		}else if(!StringUtils.isEmpty(period) && "QUARTER".equals(period)){
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentQuarterStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentQuarterEndTime() +"' ");
			
		}if(!StringUtils.isEmpty(period) && "YEAR".equals(period)){
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentYearStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentYearEndTime() +"' ");
			
		}
		
		return genericJdbcSupport.queryForInt(sentence.toString());
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.AdvertisementService#list(com.demo2do.core.web.resolver.Page)
     */
	public List<Map<String,Object>> list(Page page) {
		List<Map<String,Object>> advertisements = genericJdbcSupport.queryForList("FROM Advertisement", page.getBeginIndex(), page.getMaxResultRecords());
		page.calculate(advertisements.size());
		return advertisements.size() > page.getEveryPage() ? advertisements.subList(0, page.getEveryPage()) : advertisements;
	}
	

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.AdvertisementService#list(com.demo2do.core.web.resolver.Page, com.demo2do.boyle.common.entity.AuditStatus)
	 */
	public List<Map<String, Object>> listByPeriod(Page page, String period) {
		
		List<Map<String, Object>>  adusers = new ArrayList<Map<String, Object>>();    
		
		StringBuffer sentence = new StringBuffer();
		sentence.append(" SELECT aduser.id,aduser.user_name AS userName,aduser.nick_name AS nickName,aduser.company,aduser.tel,aduser.aduser_level_id as aduserLevelId,aduser.create_time AS createTime,aduser.is_deleted isDeleted ");
		sentence.append(" FROM aduser ");
		sentence.append(" WHERE aduser.status = 1 ");

		DateUtils dateUtils = new DateUtils();
		
		if (StringUtils.isEmpty(period) || "MONTH".equals(period)) {
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentMonthStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentMonthEndTime() +"' ");
			
		}else if(!StringUtils.isEmpty(period) && "QUARTER".equals(period)){
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentQuarterStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentQuarterEndTime() +"' ");
			
		}if(!StringUtils.isEmpty(period) && "YEAR".equals(period)){
			
			sentence.append(" and aduser.create_time  >= '"+ dateUtils.getCurrentYearStartTime() +"' ");
			sentence.append(" and aduser.create_time  <= '"+ dateUtils.getCurrentYearEndTime() +"' ");
			
		}
		
		// TODO this codes needs to be optimized 
		Map<String, Object> parameters = new HashMap<String, Object>();
		adusers = genericJdbcSupport.queryForList(sentence.toString(), parameters, page.getBeginIndex(), page.getMaxResultRecords());
		page.calculate(adusers.size());
		return adusers.size() > page.getEveryPage() ? adusers.subList(0, page.getEveryPage()) : adusers;
		
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Advertisement> listRecords(Aduser aduser) {
		// TODO Auto-generated method stub
		return genericDaoSupport.searchForList(" FROM Advertisement WHERE status = 1 and aduser = :aduser", "aduser", aduser);
	}

	@Override
	public int count(Class<T> clazz) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
